<html>
<head>
<meta charset="UTF-8">
<title>内部模型查询条件修改</title>
	<link id="easyuiTheme"  href="../../static/easyui/themes/bootstrap/easyui.css" rel="stylesheet" type="text/css"/>
	<link href="../../static/easyui/themes/icon.css" rel="stylesheet" type="text/css"/>
	<link href="../../static/css/esaas-easyui-edit.css" rel="stylesheet" type="text/css"/>
	<script src="../../static/easyui/jquery.min.js" type="text/javascript"></script>
	<script src="../../static/easyui/jquery.cookie.js" type="text/javascript"></script>
	<script src="../../static/easyui/ui-theme.js" type="text/javascript"></script>
	<script src="../../static/easyui/jquery.easyui.min.js" type="text/javascript" charset="utf-8"></script>
	<script src="../../static/easyui/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
	<script src="../../static/plugin/assets/jslib.util.js" type="text/javascript"></script>
	<script src="../../static/plugin/assets/jslib.jquery.ajax.js" type="text/javascript"></script>
	<script src="../../static/plugin/assets/jslib.easyui.util.js" type="text/javascript"></script>
	<script src="../../ffres/dict/all.js" type="text/javascript"></script>
	<a href="" target="_blank" id="commonLink" name="commonLink"></a>
<style>
	body {
		overflow: auto !important
	}
	#ff table td {
		vertical-align:top
	}
</style>
</head>
<body class="easyui-layout" scroll="no">
	<form id="ff" method="post">
		<div class="esaas-info-title">内部模型查询条件信息</div>
		<table class="esaas-info-edit-table">
			<tr hidden="true">
				<td width="15%" align="right">条件标识：</td>
				<td width="35%"><input class="easyui-textbox" type="text"
					name="condId" data-options="required:false"
					style="width: 100%; height: 25px;"></input></td>
				<td width="15%" align="right">应用模型标识：</td>
				<td width="35%"><input class="easyui-textbox" type="text"
					name="appModelId" data-options="required:false" id="appModelId"
					style="width: 100%; height: 25px;"></input></td>
			</tr>
			<tr hidden="true">
				<td width="15%" align="right">条件字段代码：</td>
				<td width="35%"><input class="easyui-textbox" type="text"
					name="condFieldCode" data-options="required:false"
					style="width: 100%; height: 25px;"></input></td>
				<td width="15%" align="right">条件字段名称：</td>
				<td width="35%"><input class="easyui-textbox" type="text"
					name="condFieldName" data-options="required:false"
					style="width: 100%; height: 25px;"></input></td>
			</tr>
			<tr>
				<td width="15%" align="left">条件逻辑关系：</td>
				<td width="20%"><select class="easyui-combobox"
					id="condLogicRelation" name="condLogicRelation"
					style="width: 100%; height: 25px;"
					data-options="required:false,editable:false,valueField:'enname',textField:'cnname',data:COND_LOGIC_RELATION">
				</select></td>
				<td></td>
				<td></td>
			</tr>
			<tr>
				<td width="15%" align="left">条件字段：</td>
				<td width="15%" align="left">比较符：</td>
				<td width="15%" align="left">比较目标类型：</td>
				<td width="15%" align="left" class="paramField">输入参数：</td>
				<td width="15%" align="left" class="constValue">常量值：</td>
			</tr>
			<tr>
				<td width="35%">
					<ul class="easyui-datalist" id="condFieldList" data-options="valueField:'paramFieldCode', textField:'text'"></ul>
				</td>
				<td width="20%"><select class="easyui-combobox"
					id="condCompareSign" name="condCompareSign"
					style="width: 100%; height: 25px;"
					data-options="required:false,editable:false,valueField:'enname',textField:'cnname',data:COND_COMPARE_SIGN">
				</select></td>
				<td width="20%"><select class="easyui-combobox"
					id="compareTargetType" name="compareTargetType"
					style="width: 100%; height: 25px;"
					data-options="required:false,editable:false,valueField:'enname',textField:'cnname',data:COMPARE_TARGET_TYPE, onChange:compareTargetChange">
				</select></td>
				<td width="35%" class="paramField">
					<ul class="easyui-datalist" id="paramFieldList" data-options="valueField:'paramFieldCode', textField:'text'"></ul>
				</td>
				<td width="35%" class="constValue"><input class="easyui-textbox" type="text"
					name="constValue" data-options="required:false"
					style="width: 100%; height: 25px;"></input></td>
			</tr>
			<tr hidden="true">
				<td width="15%" align="right">参数名称：</td>
				<td width="35%"><input class="easyui-textbox" type="text"
					name="paramFieldName" data-options="required:false"
					style="width: 100%; height: 25px;"></input></td>
				<td width="15%" align="right">参数代码：</td>
				<td width="35%"><input class="easyui-textbox" type="text"
					name="paramFieldCode" data-options="required:false"
					style="width: 100%; height: 25px;"></input></td>
			</tr>
		</table>
	</form>
	<div style="text-align: center; padding: 15px">
		<a href="javascript:void(0)" class="easyui-linkbutton"
			onclick="submitForm()">&nbsp;保&nbsp;存&nbsp;</a> <a
			href="javascript:void(0)" class="easyui-linkbutton"
			onclick="closeForm()">&nbsp;关&nbsp;闭&nbsp;</a>
	</div>
	<script>
		var appId = $.getUrlParam('appId'), appModelId;
		$(function() {
			var condId = $.getUrlParam('condId');
			debugger
			$.restfulGet('../../api/isrv/cond/info/' + condId,null,function(data){
					$('#ff').form('load', {
						condId : data.rows.condId,
						appModelId : data.rows.appModelId,
						condFieldCode : data.rows.condFieldCode,
						condFieldName : data.rows.condFieldName,
						condLogicRelation : data.rows.condLogicRelation,
						condCompareSign : data.rows.condCompareSign,
						compareTargetType : data.rows.compareTargetType,
						constValue : data.rows.constValue,
						paramFieldName : data.rows.paramFieldName,
						paramFieldCode : data.rows.paramFieldCode
					});					
					appModelId = data.rows.appModelId
					loadTableFields(appModelId, data.rows)
					compareTargetChange(data.rows.compareTargetType) // hide constValue for init
			});
		});
	
		function submitForm() {
			if (!$('#ff').form('validate'))
				return;
			var jsonData = $("#ff").serializeJson();

			var condFieldSelect = $('#condFieldList').datagrid('getSelected');
			if (!condFieldSelect) {
				$.messager.alert('提示','请选择一条条件字段！','info');
				return;
			}

			jsonData.condFieldCode = condFieldSelect.paramFieldCode;
			jsonData.condFieldName = condFieldSelect.paramFieldName;

			if (jsonData.compareTargetType == "2") {
				var paramFieldSelect = $('#paramFieldList').datagrid('getSelected');
				if (!paramFieldSelect) {
					$.messager.alert('提示','请选择一条输入参数！','info');
					return;
				}
				
				jsonData.paramFieldCode = paramFieldSelect.paramFieldCode;
				jsonData.paramFieldName = paramFieldSelect.paramFieldName;
			}

			$.restfulPut('../../api/isrv/cond/info/',jsonData, function(data) {
				window.parent.reloadList();
			});
		};

		function loadTableFields(appModelId, model) {
			var objectId, selectIndex = -1 ;
			$.restfulGet('../../api/app/model/info/' + appModelId,null,function(data){
				objectId = data.rows.tableModelId;
				var url = `../../api/model/table/fields`;
				$.restfulPostForm(url, {
					objectId: objectId ,
					size: 100
				}, function(data) {
					var rows = data.rows;
					if (rows && rows.length > 0 ) {
						rows.forEach( (f, index) => {
							f.paramFieldCode = f.fieldCode;
							f.paramFieldName = f.fieldName;
							f.text = `${f.fieldCode} ${f.fieldName ? f.fieldName : ''}`;
							if (model.condFieldCode && model.condFieldCode == f.paramFieldCode) {
								selectIndex = index
							}
						})
						$('#condFieldList').datalist({data: rows})
						if (selectIndex != -1) {
							$('#condFieldList').datalist("selectRow", selectIndex);
						}
					}
					
				});
			});
			
		}

		function loadIsrvInoutInfos(appModelId, model) {
			var selectIndex = -1;
			$.restfulPostForm(`../../api/isrv/inout/infos`, {
				ioType: "I",
				appModelId : appModelId
			}, function(data){
				data.rows.forEach((f, index) =>{
					f.text = `${f.paramFieldCode} ${f.paramFieldName ? f.paramFieldName: "" }`;
					if (model.paramFieldCode && model.paramFieldCode == f.paramFieldCode) {
						selectIndex = index
					}
				})
				$('#paramFieldList').datalist({data: data.rows})
				if (selectIndex != -1) {
					$('#paramFieldList').datalist("selectRow", selectIndex);
				}
			})
		}
		
		function compareTargetChange(newValue, oldValue) {
			if (newValue == "2") {
				$('.constValue').hide();
				$('.paramField').show();
				loadIsrvInoutInfos(appModelId, {})
			} else {
				// 常量
				$('.constValue').show();
				$('.paramField').hide();
			}
		}
		
		function closeForm(){
			window.parent.JLEUtil.closewin();
		}
	</script>
</body>
</html>
